package org.csu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.csu.dao.AdminDao;
import org.csu.dao.SignonDao;
import org.csu.domain.Signon;
import org.csu.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YinBo
 * @since 2025-03-08
 */
@Controller
@RequestMapping("/tologin")
public class SignonController {
    @Autowired
    private IAdminService adminService;

//    @GetMapping("/login")
//    public String loginPage() {
//        System.out.println("man");
//        return "login";
//    }

    //跳转页面
    @Autowired
    private SignonDao signonDao;
    @PostMapping("/login")
    public String login(@RequestParam String username,
                        @RequestParam String password, Model model,
                        RedirectAttributes redirectAttributes, HttpSession session) {

        System.out.println("kobe");
        QueryWrapper<Signon> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username).eq("password", password);
        Signon signon = signonDao.selectOne(queryWrapper);
        if (signon != null) {
            //model.addAttribute("username", username);
            redirectAttributes.addFlashAttribute("username", username);
            session.setAttribute("signon", signon);
            return "redirect:/pages/categoryManager.html";
        }
        else {;
            //model.addAttribute("message", "用户名或密码错误");
            redirectAttributes.addFlashAttribute("message", "用户名或密码错误");
            return "redirect:/index.html";
        }
    }

//    @GetMapping("/pages/categoryManager.html")
//    public String Page() {
//        return "/pages/categoryManager.html";
//    }

}
